/*
 * Copyright (c) Mirth Corporation. All rights reserved.
 * 
 * http://www.mirthcorp.com
 * 
 * The software in this package is published under the terms of the MPL license a copy of which has
 * been included with this distribution in the LICENSE.txt file.
 */

package com.mirth.connect.client.ui;

import java.awt.datatransfer.Transferable;

import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JList;

import com.mirth.connect.client.ui.VariableListHandler.TransferMode;
import com.mirth.connect.util.TcpUtil;

public class ByteAbbreviationList extends javax.swing.JPanel {

    /**
     * Creates new form ByteAbbreviationList
     */
    public ByteAbbreviationList() {
        initComponents();
        mirthVariableList.setTransferMode(TransferMode.RAW);

        DefaultListModel model = new DefaultListModel();
        for (int i = 0; i <= 127; i++) {
            model.addElement(TcpUtil.convertHexToAbbreviation(Integer.toHexString(i).toUpperCase()));
        }
        mirthVariableList.setModel(model);

        mirthVariableList.setTransferHandler(new VariableListHandler(TransferMode.RAW) {
            @Override
            protected Transferable createTransferable(JComponent c) {
                int index = ((JList) c).getSelectedIndex();
                if (index >= 0) {
                    return new VariableTransferable(String.format("%02x", index).toUpperCase(), TransferMode.RAW);
                } else {
                    return null;
                }
            }
        });
    }

    // @formatter:off
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        mirthVariableList = new com.mirth.connect.client.ui.components.MirthVariableList();

        setBackground(new java.awt.Color(255, 255, 255));
        setBorder(javax.swing.BorderFactory.createTitledBorder("Byte Abbreviations"));

        jScrollPane1.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));

        mirthVariableList.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        mirthVariableList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(mirthVariableList);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 253, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // @formatter:on

    // Variables declaration - do not modify//GEN-BEGIN:variables

    private javax.swing.JScrollPane jScrollPane1;
    private com.mirth.connect.client.ui.components.MirthVariableList mirthVariableList;
    // End of variables declaration//GEN-END:variables
}
